package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.GroupInfo;
import ywpt.service.config.GroupService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class GroupInfoAction  extends BaseAction implements
ModelDriven<GroupInfo>, Preparable{

	/**
	 * 工作组Action
	 */
	private static final long serialVersionUID = -4347501561382483496L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private Long departId;
	private String departName;
	
	private GroupInfo groupInfo;
	private GroupService  groupService;
	private List<GroupInfo> groupInfoList = new ArrayList<GroupInfo>();
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public String getDepartName() {
		return departName;
	}

	public void setDepartName(String departName) {
		this.departName = departName;
	}

	public GroupInfo getGroupInfo() {
		return groupInfo;
	}

	public void setGroupInfo(GroupInfo groupInfo) {
		this.groupInfo = groupInfo;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public List<GroupInfo> getGroupInfoList() {
		return groupInfoList;
	}

	public void setGroupInfoList(List<GroupInfo> groupInfoList) {
		this.groupInfoList = groupInfoList;
	}

	@Override
	public GroupInfo getModel() {
		return this.groupInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id !=null){
			this.groupInfo = groupService.getGroupInfoById(id);
		}else{
			this.groupInfo = new GroupInfo();
		}
	}

	// **************************数据交互方法*******************************

	@SuppressWarnings("unchecked")
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(GroupInfo.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = groupService.getGroupInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		groupInfoList.addAll(groupService.getGroupInfos(query, pageSize, rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		String code =this.groupService.createGroupCode();
		groupInfo.setCode(code);
		this.setDepartId(departId);
		return INPUT;
	}
	
	public String edit() 
	{
		this.groupInfo=this.groupService.getGroupInfoById(id);
		return INPUT;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{

		try 
		{
			if (id == null) 
			{
				groupService.saveGroupInfo(groupInfo,departId);
			} 
			else 
			{
				groupService.updateGroupInfo(groupInfo,departId);
			}
			this.addActionMessage("信息已保存!");
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
//			DetachedCriteria contractQuery = DetachedCriteria.forClass(ContractInfo.class);
//			contractQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows1 = contractService.getContractInfoCountByCriteria(contractQuery);			
//
//			DetachedCriteria jobQuery = DetachedCriteria.forClass(JobInfo.class);
//			jobQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows2 = jobService.getJobInfoCountByCriteria(jobQuery);
//			
//			DetachedCriteria deviceQuery = DetachedCriteria.forClass(DeviceInfo.class);
//			deviceQuery.add(Restrictions.eq("client", clientService.getClientInfoById(id)));
//			int	totalRows3 = deviceService.getDeviceInfoCountByCriteria(deviceQuery);
			
//			if(totalRows1>0 || totalRows2>0 || totalRows3>0)
//			{
//				this.addActionMessage("该客户已经发生过业务逻辑，不能删除！");
//				return INPUT;
//			}
			groupService.removeGroupInfo(id);
			return SUCCESS;
		}
		this.addActionMessage("编号为空，请选择具体的记录！");
		return INPUT;
	}
	
	public String select() 
	{
		groupInfoList.addAll(groupService.getGroupInfos());
		return SUCCESS;
	}
}
